<?php

class Flickr extends AppModel
{

var $name ='Flickr';
var $useTable = false;	

/* Your Flickr Api Key ( Request one :
http://www.flickr.com/services/api/keys/  )	*/
var $flickr_api_key = '90313718fff92233b7ac2cb017a8bd30';

/* You can find your Flickr NSID hovering your mouse over the RSS links of your
photos, it looks like xxxxxxx@Nyy */
var $flickr_id = '9197273@N04';

/* You don't need to change this, it keeps me from writing this n times */
var $flickr_api_url ='http://api.flickr.com/services/rest/?method=flickr.';


/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  rest_to_xml
 *  Description:  Takes an API call as a parameter, and returns the matching 
 *		  simplexml object.
 *		  All functions of this model are based on that function.
 * =====================================================================================
 */
function rest_to_xml ($method,$attributes) {

	$url_query = $this->flickr_api_url.$method.'&api_key='.$this->flickr_api_key.$attributes;
	$output = simplexml_load_file($url_query);

return $output;
}



/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  photosets
 *  Description:  Returns a 2x level array, each photoset has a : 
 *  title, description, id, icon ( thumbnail src ), title_url ( title with blanks replaced 
 *  by - ).
 * =====================================================================================
 */
function photosets() 
{

	$xml = $this->rest_to_xml("photosets.getList",'&user_id='.$this->flickr_id);

	$i = 0;
	foreach ($xml->photosets->photoset as $photoset )
		{
		$array[$i]['title'] = $photoset->title;
		$array[$i]['description'] = $photoset->description;
		$array[$i]['id'] = $photoset['id'];	
		$array[$i]['title_url'] = ereg_replace(' ','-',$photoset->title);
		$array[$i]['link'] = 'sets/photos/'.$photoset['id'].'/'.$array[$i]['title_url'];	
		$xml_2 = $this->rest_to_xml("photos.getSizes",'&photo_id='.$photoset['primary']);

		foreach($xml_2->sizes->size as $icon)
			{
			if ( $icon['label'] == "Square")
				{
				$array[$i]['icon'] = $icon['source'];
				}
			}
		
		
		$i++;
		}
return $array;
}

/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  photos
 *  Description:  Returns a 2x level array, each photo has a :
 *		  url
 * =====================================================================================
 */
function photos($id)
{
	$xml = $this->rest_to_xml("photosets.getPhotos",'&photoset_id='.$id.'&per_page=20');
	
	$i = 0;
	foreach($xml->photoset->photo as $photo)
		{
		$xml_2 = $this->rest_to_xml("photos.getSizes",'&photo_id='.$photo['id']);
		foreach($xml_2->sizes->size as $size)
			{
			if ( $size['label'] == "Square")
				{
				$array[$i]['url'] = $size['source'];
				}
			}
		$i++;
		}
return $array;
}

} // End Model Class
?>
